//
//  DKMainBankCardModifyController.m
//  DanKU
//
//  Created by 蓝鳍互娱 on 2023/12/2.
//

#import "DKMainBankCardModifyController.h"
#import "DKMainBankCardInputErrorModel.h"
#import "DKBankCardNewController.h"
@interface DKMainBankCardModifyController ()<UITextFieldDelegate>
@property (nonatomic,strong) UIView * dk_BankCardModifyView;
@property (nonatomic,strong) UITextField *dk_BankCardModifyField;
@property (nonatomic,strong) UIButton *dk_BankCardModifyBtn;
@property (nonatomic,strong) UILabel *dk_BankCardModifyAlertLab;

@property (nonatomic,strong) DKMainBankCardInputErrorModel *dk_MainBankCardInputErrorMdl;
@end

@implementation DKMainBankCardModifyController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    [self dk_addSubView];
    [self makeNavBack];
}
- (void)dk_addSubView{
    UIImageView * dk_BankCardHeadBg = [[UIImageView alloc] init];
    dk_BankCardHeadBg.userInteractionEnabled = YES;
    dk_BankCardHeadBg.image = DK_GetImage(@"dk_MainBankCardHeadBg");
    [self.view addSubview:dk_BankCardHeadBg];
    [dk_BankCardHeadBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(0)));
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(DK_IsWidthScale(0)));
        make.height.equalTo(@(DK_IsWidthScale(200)));
    }];
    self.dk_BankCardModifyView = [[UIView alloc] init];
    self.dk_BankCardModifyView.backgroundColor = DK_Hex(@"#FFFFFF");
    [self.view addSubview:self.dk_BankCardModifyView];
    [self.dk_BankCardModifyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(120)+DK_SafeAreaTop));
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(-DK_IsWidthScale(0)));
        make.bottom.equalTo(@(DK_IsWidthScale(0)));
    }];
    UILabel * dk_BankCardModifyHeadLab = [[UILabel alloc] init];
    dk_BankCardModifyHeadLab.textColor = DK_Hex(@"#999999");
    dk_BankCardModifyHeadLab.font = DK_FONT_Regular(14);
    dk_BankCardModifyHeadLab.text = @"Original bank account number";
    [self.dk_BankCardModifyView addSubview:dk_BankCardModifyHeadLab];
    [dk_BankCardModifyHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DK_IsWidthScale(30)));
        make.left.equalTo(@(DK_IsWidthScale(45)));
        make.height.equalTo(@(DK_IsWidthScale(20)));
    }];
    UIView * dk_BankCardModifyView = [[UIView alloc] init];
    dk_BankCardModifyView.layer.cornerRadius = DK_IsWidthScale(14);
    dk_BankCardModifyView.backgroundColor = DK_Hex(@"#F7F7F8");
    [self.dk_BankCardModifyView addSubview:dk_BankCardModifyView];
    [dk_BankCardModifyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(dk_BankCardModifyHeadLab.mas_bottom).offset(DK_IsWidthScale(10));
        make.height.equalTo(@(DK_IsWidthScale(50)));
    }];
    self.dk_BankCardModifyField = [[UITextField alloc] init];
    self.dk_BankCardModifyField.font = DK_FONT_Medium(14);
    self.dk_BankCardModifyField.placeholder = @"Please enter";
    self.dk_BankCardModifyField.delegate = self;
    [dk_BankCardModifyView addSubview:self.dk_BankCardModifyField];
    [self.dk_BankCardModifyField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(15)));
        make.right.equalTo(@(-DK_IsWidthScale(15)));
        make.top.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(@(DK_IsWidthScale(0)));
    }];
    self.dk_BankCardModifyAlertLab = [[UILabel alloc] init];
    self.dk_BankCardModifyAlertLab.textColor = DK_Hex(@"#EC726E");
    self.dk_BankCardModifyAlertLab.font = DK_FONT_Regular(12);
    self.dk_BankCardModifyAlertLab.numberOfLines = 0;
    self.dk_BankCardModifyAlertLab.hidden = YES;
    [self.dk_BankCardModifyView addSubview:self.dk_BankCardModifyAlertLab];
    [self.dk_BankCardModifyAlertLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dk_BankCardModifyView.mas_bottom).offset(DK_IsWidthScale(8));
        make.left.equalTo(@(DK_IsWidthScale(45)));
        make.right.equalTo(@(-DK_IsWidthScale(45)));
    }];
    self.dk_BankCardModifyBtn = [[UIButton alloc] init];
    [self.dk_BankCardModifyBtn setTitle:@"Confirm" forState:UIControlStateNormal];
    [self.dk_BankCardModifyBtn setTitleColor:DK_Hex(@"#4566FF") forState:UIControlStateDisabled];
    [self.dk_BankCardModifyBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateNormal];
    self.dk_BankCardModifyBtn.titleLabel.font = DK_FONT_Semibold(16);
    [self.dk_BankCardModifyBtn setBackgroundImage:DK_GetImage(@"dk_registerNextSelectBg") forState:UIControlStateNormal];
    [self.dk_BankCardModifyBtn setBackgroundImage:DK_GetImage(@"dk_registerNextBg") forState:UIControlStateDisabled];
    self.dk_BankCardModifyBtn.enabled = NO;
    [self.dk_BankCardModifyBtn addTarget:self action:@selector(bankCardModify) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.dk_BankCardModifyBtn];
    [self.dk_BankCardModifyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.top.equalTo(self.dk_BankCardModifyAlertLab.mas_bottom).offset(DK_IsWidthScale(30));
    }];
    [self getBankCardErrorNum];
}
- (void)getBankCardErrorNum{
    NSData *bankCardErrorData = DK_GETObject_USERDEF(NSUserDefault_BankCardError);
    self.dk_MainBankCardInputErrorMdl = [NSKeyedUnarchiver unarchiveObjectWithData:bankCardErrorData];
    if(!self.dk_MainBankCardInputErrorMdl||![self.dk_MainBankCardInputErrorMdl.currentTime isEqualToString:[NSString getCurrentTimeDayTimestamp]]){
        self.dk_MainBankCardInputErrorMdl = [[DKMainBankCardInputErrorModel alloc] init];
    }
}
- (void)saveBankCardErrorNum{
    NSData *bankCardErrorData = [NSKeyedArchiver archivedDataWithRootObject:self.dk_MainBankCardInputErrorMdl requiringSecureCoding:false error:nil];
    DK_SETObject_USERDEF(NSUserDefault_BankCardError, bankCardErrorData);
}
- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    [self.dk_BankCardModifyView makeCornerOnTopleft:DK_IsWidthScale(50)];
}
- (void)bankCardModify{
   //验证银行卡号
    if([self.dk_BankCardModifyField.text isEqualToString:self.dk_BankCardMdl.bankCardInfo.bankCode]&&[self.dk_MainBankCardInputErrorMdl.errorNum intValue] < 3){//验证一致
        DKBankCardNewController *bankCardNewCtrl = [[DKBankCardNewController alloc] init];
        [self.navigationController pushViewController:bankCardNewCtrl animated:YES];
    }else{//输入错误
        self.dk_BankCardModifyAlertLab.hidden = NO;
        if([self.dk_MainBankCardInputErrorMdl.errorNum intValue] >= 3){
            self.dk_BankCardModifyAlertLab.text = @"*The bank card number has been entered incorrectly 3 times today, the card number cannot be changed temporarily, please try tomorrow";
        }else{
            self.dk_BankCardModifyAlertLab.text = @"*The original bank card number is wrong, please re-enter";
            self.dk_MainBankCardInputErrorMdl.errorNum = [NSString stringWithFormat:@"%d",[self.dk_MainBankCardInputErrorMdl.errorNum intValue]+1];
            [self saveBankCardErrorNum];
        }
        self.dk_BankCardModifyBtn.enabled = NO;
    }
}
#pragma mark UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString * dk_PanCardMenberText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if(dk_PanCardMenberText.length > 0){
        self.dk_BankCardModifyBtn.enabled = YES;
    }else{
        self.dk_BankCardModifyBtn.enabled = NO;
    }
    self.dk_BankCardModifyAlertLab.text = @"";
    return YES;
}
@end
